package com.example.myapplication.ViewPageFragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

//视图页面片段活动
public class ViewPageFragmentActivity extends AppCompatActivity implements View.OnClickListener{
    String Tab = "ViewPageFragmentActivity";
    //视图翻页器
    ViewPager2 viewPager2;
    //线形布局
    private LinearLayout llChat,llContacts,llFind,llProfile;
    //图片视图
    private ImageView ivChat,ivContacts,ivFind,ivProfile,ivCurrent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_page_fragment);
        ViewPager2 viewPager2 = findViewById(R.id.view_page);

        List<ViewPager2Adapter.PageItem> pageItems = new ArrayList<>();
        pageItems.add(new ViewPager2Adapter.PageItem("1",R.color.red));
        pageItems.add(new ViewPager2Adapter.PageItem("2",R.color.blue));
        pageItems.add(new ViewPager2Adapter.PageItem("3",R.color.purple_200));
        pageItems.add(new ViewPager2Adapter.PageItem("4",R.color.white));
        pageItems.add(new ViewPager2Adapter.PageItem("5",R.color.black));
        pageItems.add(new ViewPager2Adapter.PageItem("6",R.color.teal_200));
        ViewPager2Adapter viewPager2Adapter = new ViewPager2Adapter(pageItems);
        viewPager2.setAdapter(viewPager2Adapter);
//        initPager();
        initTabView();
    }
    //初始化4组图片、文字视图
    private void initTabView() {
        llChat = findViewById(R.id.id_tab_weixin);
        llContacts = findViewById(R.id.id_tab_contact);
        llFind = findViewById(R.id.id_tab_find);
        llProfile = findViewById(R.id.id_tab_mine);

        ivChat = findViewById(R.id.id_tab_weixin_image);
        ivContacts = findViewById(R.id.id_tab_contact_image);
        ivFind = findViewById(R.id.id_tab_find_image);
        ivProfile = findViewById(R.id.id_tab_mine_image);

        llChat.setOnClickListener(this);
        llContacts.setOnClickListener(this);
        llFind.setOnClickListener(this);
        llProfile.setOnClickListener(this);

        //默认选择通讯Tab
        ivChat.setSelected(true);
        ivCurrent = ivChat;
    }

    //初始化视图翻页器
    private void initPager() {
        viewPager2 = findViewById(R.id.view_page);
        //片段数组
        ArrayList<Fragment> fragments=new ArrayList<>();
        //加入片段（空白片段.新建实例（"参数"））
        fragments.add(BlankFragment.newInstance("微信"));
        fragments.add(BlankFragment.newInstance("通信路"));
        fragments.add(BlankFragment.newInstance("發現"));
        fragments.add(BlankFragment.newInstance("我"));
        //翻页适配器（获取支持片段的管理者，获取生命周期，片段数组）
        ViewPager2Adapter2 viewPager2Adapter2 = new ViewPager2Adapter2(getSupportFragmentManager(),getLifecycle(),fragments);
        //视图翻页器.设置适配器
        viewPager2.setAdapter(viewPager2Adapter2);
        ///视图翻页器.注册页面改变回调
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            //在页面滚动结束时（位置，位置偏移，位置偏移像素）
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            //在页面选择时
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            ///在页面滚动状态改变
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    //改变Tab
    private void changeTab(int position) {
        ///当前选择变为false
        ivCurrent.setSelected(false);
        Log.d(Tab, "========="+position);
        switch (position){
            case R.id.id_tab_weixin:
                viewPager2.setCurrentItem(0);
            case 0:
                ivChat.setSelected(true);
                ivCurrent = ivChat;
                break;
            case R.id.id_tab_contact:
                viewPager2.setCurrentItem(1);
            case 1:
                ivContacts.setSelected(true);
                ivCurrent = ivContacts;
                break;
            case R.id.id_tab_find:
                viewPager2.setCurrentItem(2);
            case 2:
                ivFind.setSelected(true);
                ivCurrent = ivFind;
                break;
            case R.id.id_tab_mine:
                viewPager2.setCurrentItem(3);
            case 3:
                ivProfile.setSelected(true);
                ivCurrent = ivProfile;
                break;
        }
    }

    @Override
    //重写点击事件
    public void onClick(View v) {
        changeTab(v.getId());
    }
}